﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using FundamentalsOfCompiling;

namespace CourseDesign_01_LexicalAnalysis
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        string fileSourcePath = string.Empty;

        public MainWindow()
        {
            InitializeComponent();
        }

        //  Menu_File_Open_Click
        private void Menu_File_Open_Click(object sender, RoutedEventArgs e)
        {
            string fileSourceContent = null;
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = ".txt";
            ofd.Filter = "源文件(*.txt)|*.txt|全部文件(*.*)|*.*";
            //ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
            if (ofd.ShowDialog() == true)
            {
                fileSourcePath = ofd.FileName;

                using (var fileSourceStream = new System.IO.StreamReader(ofd.FileName, Encoding.Default))
                {
                    fileSourceContent = fileSourceStream.ReadToEnd();
                    this.richTextBox1.AppendText(fileSourceContent);
                }   //  using
            }   //  ofd.ShowDialog

        }   //Menu_File_Open_Click

        //  Menu_File_Save_Click
        private void Menu_File_Save_Click(object sender, RoutedEventArgs e)
        {
            //系统路径-涉及不到的可以去掉
            //string strPath = Environment.CurrentDirectory;
            //string strPath = this.fileSourcePath;
            Microsoft.Win32.SaveFileDialog dialogSaveFile = new Microsoft.Win32.SaveFileDialog();
            dialogSaveFile.DefaultExt = "txt";//默认扩展名
            dialogSaveFile.AddExtension = true;//是否自动添加扩展名
            dialogSaveFile.Filter = "文本文件(*.txt)|.txt|自定义文件(*.*)|*.*";
            dialogSaveFile.OverwritePrompt = true;//文件已存在是否提示覆盖
            dialogSaveFile.FileName = "分析结果";//默认文件名
            dialogSaveFile.CheckPathExists = true;//提示输入的文件名无效
            dialogSaveFile.Title = "保存文件";

            //显示对话框
            bool? b = dialogSaveFile.ShowDialog();
            if (b == true)//点击保存
            {
                string fileOutput = dialogSaveFile.FileName;
                TextRange textRange = new TextRange(this.richTextBox2.Document.ContentStart, this.richTextBox2.Document.ContentEnd);

                string outputContent = textRange.Text;
                //string outputContent = this.richTextBox2.ToString();
                try
                {
                    Stream stream = File.OpenWrite(fileOutput);
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.Write(outputContent);
                        writer.Close();
                    }
                    stream.Close();
                }
                catch (IOException ex)
                {
                    this.outPut(ex.Message);
                }

            }
        }   //Menu_File_Save_Click

        //  Menu_Analysis_LexicalAnalysis_Click
        private void Menu_Analysis_LexicalAnalysis_Click(object sender, RoutedEventArgs e)
        {
            //Console.WriteLine("main is beginning");
            MessageBox.Show("LexicalAnalysis Is Beginning ... ");
            Global gl = Global.createGlobal();
            WordParser paser = new WordParser(gl, this);
            paser.getTokenList(this.fileSourcePath);
            paser.printTokenlist();
        }   //Menu_Analysis_LexicalAnalysis_Click

        public void outPut(String str)
        {
            this.richTextBox2.AppendText(str);
        }

        public void outPutLine(String str)
        {
            this.richTextBox2.AppendText(str + "\n");
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            TextRange tr = new TextRange(this.richTextBox1.Document.ContentStart, this.richTextBox1.Document.ContentEnd);
            tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.White));
            TextPointer cp = this.richTextBox1.CaretPosition;
            tr = new TextRange(cp, this.richTextBox1.Document.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward));
            if (tr.IsEmpty)
                MessageBox.Show("This is the end");
            tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.SkyBlue));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            /*
            TextRange tr = new TextRange(this.richTextBox1.CaretPosition, this.richTextBox1.CaretPosition);
            var value = tr.GetPropertyValue(TextElement.ForegroundProperty);
            tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Red));
            tr.ApplyPropertyValue(TextElement.BackgroundProperty, (SolidColorBrush)value);
            */
            TextPointer tpend = this.richTextBox1.CaretPosition;
            TextPointer tpstart = tpend.GetPositionAtOffset(5);
            TextRange tr = new TextRange(tpstart, tpend);
            tr.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Red));

        }

        private void richTextBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            //MessageBox.Show(e.Changes.);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("F:\\MV\\小小 - 容祖儿.flv");
            //MessageBox.Show("Button_Click");
        }
    }
}
